#ifndef _IALIGNMENT_H_
#define _IALIGNMENT_H_

#include <BRect>
#include <BFont>

namespace BWE
{
	template<class T = int>
	class IAlignmentX
	{
	public:
		IAlignmentX()
		{
			ia_form = Form_None;
			ia_align = Align_None;
			ia_spacing = 1;
		}
		~IAlignmentX()
		{

		}

		BRectX<T> adjust(const BRectX<T>& area)
		{
			ia_area = area;
			ia_hintSize.reset();
			ia_textRect.reset();
			ia_iconRect.reset();
			ia_textArea.reset();
			Form realForm = ia_form;
			if (ia_form == Form_None)
			{
				if (ia_iconSize.valid() && ia_textSize.valid())
				{
					realForm = Form_IconText;
				}
				else
				{
					if (ia_iconSize.valid())
						realForm = Form_IconOnly;
					else
						realForm = Form_TextOnly;
				}
			}
			if (realForm == Form_IconAlone)
			{
				if (ia_iconSize.empty())
					realForm = Form_TextOnly;
				else
					realForm = Form_IconOnly;
			}
			if (realForm == Form_TextAlone)
			{
				if (ia_textSize.empty())
					realForm = Form_IconOnly;
				else
					realForm = Form_TextOnly;
			}
			switch (realForm)
			{
			case Form_IconOnly:
			{
				ia_iconArea = area;
				ia_hintSize = ia_iconSize;
				ia_rect = ia_iconArea.align(ia_align, ia_iconSize);
				ia_iconRect = ia_rect;
				return ia_rect;
			}
			case Form_TextOnly:
			{
				ia_textArea = area;
				ia_hintSize = ia_textSize;
				ia_rect = ia_textArea.align(ia_align, ia_textSize);
				ia_textRect = ia_rect;
				return ia_rect;
			}
			case Form_IconText:
			{
				ia_hintSize.width() = ia_iconSize.width() + ia_spacing + ia_textSize.width();
				ia_hintSize.height() = bMax(ia_iconSize.height(), ia_textSize.height());
				ia_rect = area.align(ia_align, ia_hintSize);

				ia_iconArea = ia_rect;
				ia_iconArea.setWidth(ia_iconSize.width());
				ia_iconRect = ia_iconArea.align(ia_align, ia_iconSize);

				ia_textArea = ia_rect;
				ia_textArea.left() = ia_rect.right() - ia_textSize.width();
				ia_textRect = ia_textArea.align(ia_align, ia_textSize);

				return ia_rect;
			}
			case Form_TextIcon:
			{
				ia_hintSize.width() = ia_iconSize.width() + ia_spacing + ia_textSize.width();
				ia_hintSize.height() = bMax(ia_iconSize.height(), ia_textSize.height());
				ia_rect = area.align(ia_align, ia_hintSize);

				ia_textArea = ia_rect;
				ia_textArea.setWidth(ia_textSize.width());
				ia_textRect = ia_textArea.align(ia_align, ia_textSize);

				ia_iconArea = ia_rect;
				ia_iconArea.left() = ia_rect.right() - ia_iconSize.width();
				ia_iconRect = ia_iconArea.align(ia_align, ia_iconSize);

				return ia_rect;
			}
			case Form_TextUnderIcon:
			{
				ia_hintSize.width() = bMax(ia_iconSize.width(), ia_textSize.width());
				ia_hintSize.height() = ia_iconSize.height() + ia_spacing + ia_textSize.height();
				ia_rect = area.align(ia_align, ia_hintSize);

				ia_iconArea = ia_rect;
				ia_iconArea.setHeight(ia_iconSize.height());
				ia_iconRect = ia_iconArea.align(ia_align, ia_iconSize);

				ia_textArea = ia_rect;
				ia_textArea.top() = ia_rect.top() + ia_iconSize.height() + ia_spacing;
				ia_textRect = ia_textArea.align(ia_align, ia_textSize);

				return ia_rect;
			}
			case Form_IconUnderText:
			{
				ia_hintSize.width() = bMax(ia_iconSize.width(), ia_textSize.width());
				ia_hintSize.height() = ia_iconSize.height() + ia_spacing + ia_textSize.height();
				ia_rect = area.align(ia_align, ia_hintSize);

				ia_textArea = ia_rect;
				ia_textArea.setHeight(ia_textSize.height());
				ia_textRect = ia_textArea.align(ia_align, ia_textSize);

				ia_iconArea = ia_rect;
				ia_iconArea.top() = ia_rect.top() + ia_textSize.height() + ia_spacing;
				ia_iconRect = ia_iconArea.align(ia_align, ia_iconSize);

				return ia_rect;
			}
			case Form_IconTextOverlap:
			{
				ia_hintSize.width() = bMax(ia_iconSize.width(), ia_textSize.width());
				ia_hintSize.height() = bMax(ia_iconSize.height(), ia_textSize.height());
				ia_rect = area.align(ia_align, ia_hintSize);

				ia_iconArea = ia_rect;
				ia_iconRect = ia_rect.align(ia_align, ia_iconSize);

				ia_textArea = ia_rect;
				ia_textRect = ia_rect.align(ia_align, ia_textSize);

				return ia_rect;
			}
			default:
				ia_rect = area;
				return area;
			}
		}

		BRectX<T>		ia_area;
		Form			ia_form;
		Align			ia_align;
		T				ia_spacing;
		BSizeX<T>		ia_iconSize;
		BSizeX<T>		ia_textSize;

		BRectX<T>		ia_rect;
		BSizeX<T>		ia_hintSize;
		BRectX<T>		ia_iconArea;
		BRectX<T>		ia_iconRect;
		BRectX<T>		ia_textArea;
		BRectX<T>		ia_textRect;
	};
	typedef IAlignmentX<>			IAlignment;
	typedef IAlignmentX<int>		IAlignmenti;
	typedef IAlignmentX<float>		IAlignmentf;
	typedef IAlignmentX<double>		IAlignmentd;
	typedef IAlignmentX<BReal>		IRealAlignment;
}
#endif